package com.myacg.core.dao.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

/**
 * �ṩһ�����ɶ�дĳЩ�ļ�
 * @author Administrator
 *
 */
public class FileUtils {
	
	/**
	 * ������д����Ӧλ�õ��ļ���
	 * @param content
	 * @param savePath
	 * @param charset
	 * @return
	 */
	public   static boolean saveStringToFile(String content,String savePath,String charset){
		   System.err.println("IO-save:"+savePath);
			OutputStreamWriter out=null;
				try {
				    out=new OutputStreamWriter(new FileOutputStream(new File(savePath)), charset);
					out.write(content);
					out.flush();
					out.close();
					return true;
				} catch (UnsupportedEncodingException | FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			 
		return false;
	}
	
	/**
	 * ��ȡĳ��·���µ��ļ���String��
	 * @param filePath
	 * @param charset
	 * @return
	 */
	public static  String  readStringFromFile(String filePath,String charset){
		System.err.println("IO-read:"+filePath);
		if(FileUtils.isExist(filePath)){
			try {
				BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(filePath) , charset));
				StringBuffer cont=new StringBuffer();
				String str=null;
				while((str=reader.readLine())!=null){
					cont.append(str+"\r\n");
				}
				reader.close();
				return cont.toString();
			} catch (UnsupportedEncodingException | FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * ɾ��ĳ��·���µ��ļ�
	 * @param filePath
	 * @return
	 */
	public boolean deleteFile(String filePath){
		File file=new File(filePath);
		 if(file.exists() &&  file.isFile() ){
			return  file.delete();
		 }
		 return false;
	}
	
	/**
	 * �Ƿ�����ļ�
	 * @return
	 */
	public static boolean  isExist(String savePath){
		return  new File(savePath).exists();
	}
	
	
	 
	
	
}
